import Vue from 'vue'
import Vuex from 'vuex'
import { sortBy } from '../methods/utils'
// import createPersistedState from 'vuex-persistedstate' // vuex持久化

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // 支出列表
        payList: [],
        // 标签列表
        tagList: [
            {
                class: 'icon iconfont icon-sancanyisu',
                type: '三餐',
                flag: 1
            },
            {
                class: 'icon iconfont icon-lingshi',
                type: '零食',
                flag: 1
            },
            {
                class: 'icon iconfont icon-jiaotong',
                type: '交通',
                flag: 1
            },
            {
                class: 'icon iconfont icon-shenghuojiaofei',
                type: '生活缴费',
                flag: 1
            },
            {
                class: 'icon iconfont icon-canyin1',
                type: '餐饮',
                flag: 1
            },
            {
                class: 'icon iconfont icon-shenghuo1',
                type: '生活',
                flag: 1
            },
            {
                class: 'icon iconfont icon-yiliao1',
                type: '医疗',
                flag: 1
            }
        ],
        // 预算
        budget: '',
        // 收入
        income: '',
        // 存储本月各类型支出明细
        TagPayList: []
    },
    mutations: {
        // init* 从浏览器本地读取各项数据存到vuex中
        // save* 将vuex中的数据保存到本地
        initDataSource(state) {
            state.payList = JSON.parse(window.localStorage.getItem('payList') || '[]');
        },
        initTagList(state) {
            if (window.localStorage.getItem('tagList')) {
                state.tagList = JSON.parse(window.localStorage.getItem('tagList') || '[]');
            } else {
                window.localStorage.setItem('tagList',JSON.stringify(state.tagList));
            }
        },
        initTagPayList(state) {
            state.TagPayList = JSON.parse(window.localStorage.getItem('TagPayList') || '[]');
        },
        saveDataSource(state, payItem) {
            let dateExist = false // 用于判断新增支出数据对应的时间是否已经存在支出列表中
            // 属于同一天的支出合并到同一天中的tagName列表中
            state.payList.forEach((item) =>{
                if (item.date === payItem.date) {
                    // console.log(payItem)
                    let tagNameItem = {
                        class: payItem.tagName[0].class,
                        money: payItem.tagName[0].money,
                        notes: payItem.tagName[0].notes,
                        type: payItem.tagName[0].type
                    }
                    // console.log(tagNameItem)
                    item.tagName.push(tagNameItem);
                    item.amount += payItem.amount
                    dateExist = true
                }
            })
            // 当支出列表中不存在这一天时直接追加新的一条数据
            if (!dateExist) {
                state.payList.push(payItem)
            }
            // 将支出列表按照‘sort’这一属性排序
            state.payList.sort(sortBy('sort'))
            window.localStorage.setItem('payList',JSON.stringify(state.payList));
            // 更新标签权重
            state.tagList.forEach((item) =>{
                if (item.type === payItem.tagName[0].type) {
                    item.flag += 1
                }
            })
            state.tagList.sort(sortBy('flag'))
            window.localStorage.setItem('tagList',JSON.stringify(state.tagList));
        },
        saveTagList(state, tagItem) {
            state.tagList.push(tagItem)
            window.localStorage.setItem('tagList',JSON.stringify(state.tagList));
        },
        saveTagPayList(state, TagPayList) {
            state.TagPayList = TagPayList
            window.localStorage.setItem('TagPayList',JSON.stringify(state.TagPayList));
        },
        setBudget(state,budget) {
            state.budget = budget
            // console.log(state.income)
        },
        setIncome(state,income) {
            state.income = income
            // console.log(state.income)
        },

    },
    actions: {}
})
